﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace ListForms.Controls
{
    public class FormField
        : Microsoft.SharePoint.WebControls.FormField
    {
        protected override void CreateChildControls()
        {
            if (base.Field.Id == SPBuiltInFieldId.Title)
            {
                Controls.Add(new TitleField() { FieldName = base.FieldName });
                ChildControlsCreated = true;
            }
            else
            {
                base.CreateChildControls();
            }
        }
    }

    public class TitleField
        : BaseFieldControl
    {
        DropDownList _list;
        public override object Value
        {
            get
            {
                EnsureChildControls();
                return _list.SelectedValue;
            }
            set
            {
                EnsureChildControls();
                ListItem item = _list.Items.FindByText((string)value);
                if (item == null)
                {
                    item = new ListItem((string)value);
                    _list.Items.Add(item);
                }
                item.Selected = true;
            }
        }

        protected override void CreateChildControls()
        {
            _list = new DropDownList();
            _list.Items.Add("Item 1");
            _list.Items.Add("Item 2");
            _list.Items.Add("Item 3");
            Controls.Add(_list);
            ChildControlsCreated = true;
        }
    }
}
